package com.csun.cmny.provider.controller.websocket;

import com.csun.cmny.base.constant.MessageConstant;
import com.csun.cmny.provider.model.domain.DmcDeviceAlert;
import com.csun.cmny.provider.model.domain.DmcRfidEventOldman;
import com.csun.cmny.provider.model.dto.SendMessageRequest;
import com.csun.cmny.provider.model.dto.device.DeviceStatus;
import com.csun.cmny.provider.service.OpcMessageService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-11-13 15:57
 * @Description:
 */
@Component
public class WebSocketUtils {

    @Resource
    private OpcMessageService opcMessageService;

    /**
     * 通过websocket推送设备报警消息
     *
     * @param dmcDeviceAlert
     */
    public void pushWebsocketMessageWithAlert(DmcDeviceAlert dmcDeviceAlert) {

        SendMessageRequest sendMessageRequest = new SendMessageRequest();
        sendMessageRequest.setMsgCode(MessageConstant.MESSAGE_CODE_WEB_ALERT_REFRESH);
        sendMessageRequest.setGroupId(dmcDeviceAlert.getGroupId());
        sendMessageRequest.setMessage(dmcDeviceAlert.getAlertInfo());
        opcMessageService.pushWithMsgCode(sendMessageRequest);
    }

    /**
     * 通过websocket推送设备报警解除消息
     *
     * @param dmcDeviceAlert
     */
    public void pushWebsocketMessageWithAlertLift(DmcDeviceAlert dmcDeviceAlert) {

        SendMessageRequest sendMessageRequest = new SendMessageRequest();
        sendMessageRequest.setMsgCode(MessageConstant.MESSAGE_CODE_WEB_ALERT_REFRESH);
        sendMessageRequest.setGroupId(dmcDeviceAlert.getGroupId());
        sendMessageRequest.setMessage(dmcDeviceAlert.getHandleRemark());
        opcMessageService.pushWithMsgCode(sendMessageRequest);
    }

    /**
     * 通过websocket推送设备状态消息
     *
     */
    public void pushWebsocketMessageWithStatus(DeviceStatus deviceStatus) {

        SendMessageRequest sendMessageRequest = new SendMessageRequest();
        sendMessageRequest.setMsgCode(MessageConstant.MESSAGE_CODE_WEB_STATUS_REFRESH);
        sendMessageRequest.setGroupId(deviceStatus.getGroupId());
        sendMessageRequest.setMessage(deviceStatus.getMessage());
        opcMessageService.pushWithMsgCode(sendMessageRequest);
    }

    /**
     * 通过websocket推送刷门禁卡消息
     *
     * @param dmcRfidEventOldman
     */
    public void pushWebsocketMessageWithCard(DmcRfidEventOldman dmcRfidEventOldman) {

        SendMessageRequest sendMessageRequest = new SendMessageRequest();
        sendMessageRequest.setMsgCode(MessageConstant.MESSAGE_CODE_WEB_CARD_REFRESH);
        sendMessageRequest.setGroupId(dmcRfidEventOldman.getGroupId());
        sendMessageRequest.setMessage(dmcRfidEventOldman.getMessage());
        opcMessageService.pushWithMsgCode(sendMessageRequest);
    }
}
